package cn.tedu.yushougeproduct.service.impl;

import cn.tedu.yushouge.commons.ex.ServiceException;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.web.ServiceCode;
import cn.tedu.yushougeproduct.mapper.AlbumMapper;
import cn.tedu.yushougeproduct.mapper.PictureMapper;
import cn.tedu.yushougeproduct.pojo.entity.Picture;
import cn.tedu.yushougeproduct.pojo.param.PictureAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.PictureUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.*;
import cn.tedu.yushougeproduct.service.IPictureService;
import cn.tedu.yushouge.commons.util.PageInfoToPageDataConverter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PictureServiceImpl implements IPictureService {

    public PictureServiceImpl() {
        log.info("创建业务对象：PictureServiceImpl");
    }

    @Autowired
    PictureMapper pictureMapper;

    @Autowired
    AlbumMapper albumMapper;

    @Override
    public void addNew(PictureAddNewParam pictureAddNewParam) {
        log.debug("开始处理【添加图片】的业务，参数：{}", pictureAddNewParam);
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewParam, picture);
        picture.setIsCover(0);
        picture.setGmtCreate(LocalDateTime.now());

        log.debug("准备将新的图片数据写入到数据库，数据：{}", picture);
        int rows = pictureMapper.insert(picture);
        if (rows != 1) {
            String message = "添加图片失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的图片数据写入到数据库，完成！");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据ID删除图片】的业务，参数：{}", id);
        // 检查商品分类是否存在，如果不存在，则抛出异常
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = pictureMapper.deleteById(id);
        if (countById == 0) {
            String message = "删除图片失败，图片不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        log.debug("从图片表删除一条数据，完成！");
    }

    @Override
    public void setCover(Long id) {
        log.debug("开始处理【根据ID将图片设置为封面】的业务，参数：{}", id);

        PictureStandardVO picture = pictureMapper.getStandardById(id);
        if (picture == null) {
            String message = "设置封面图片失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        if (picture.getIsCover() == 1) {
            String message = "设置封面图片失败，此图片已经是所属相册的封面！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Long albumId = picture.getAlbumId();
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "设置封面图片失败，此图片的相册的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int row = updateNotCoverByAlbum(albumId);
        if (row < 1){
            String message = "设置封面图片失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }


        Picture newPicture = new Picture();
        newPicture.setId(id);
        newPicture.setIsCover(1);
        newPicture.setGmtModified(LocalDateTime.now());
        int rows = pictureMapper.updateById(newPicture);
        if (rows != 1) {
            String message = "设置封面图片失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }


    private int updateNotCoverByAlbum(Long albumId){
        log.debug("开始处理【根据相册ID将图片设置为\"非封面\"】的数据访问，参数：{}", albumId);
        Picture picture = new Picture();
        picture.setIsCover(0);
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("album_id",albumId);
        int rows = pictureMapper.update(picture,queryWrapper);
        return rows;
    }

    @Override
    public PictureStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询图片详情】的业务，参数：{}", id);
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        if (queryResult == null){
            String message = "查询图片详情失败，图片数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<PictureListItemVO> listByAlbumId(Long albumId) {
        log.debug("开始处理【根据相册ID查询图片数据列表】的业务，相册：{}", albumId);
        return pictureMapper.listByAlbumId(albumId);
    }

}
